package com.huanying.goodjob.prize_chance;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huanying.framework.BaseDao;
import com.huanying.framework.PageBean;
@Service
public class Prize_chanceServiceImpl implements Prize_chanceService{
	@Autowired
	private BaseDao dao;
	@Override
	public void add_prize_chance(Prize_chance prize_chance) throws Exception {
		// TODO Auto-generated method stub
		dao.save(prize_chance);
	}

	@Override
	public void update_prize_chancestatus(int id,int status) throws Exception {
		// TODO Auto-generated method stub
		
		String hql="update Prize_chance set status=? where id=?";
		
		List<Object> param=new ArrayList<Object>();
		param.add(status);
		param.add(id);
	    dao.exculeByParams(hql, param);
	}
	public void update_prize_tatus(int id,int chance_status) throws Exception {
		// TODO Auto-generated method stub
		
		String hql="update Prize_chance set chance_status=? where id=?";
		
		List<Object> param=new ArrayList<Object>();
		param.add(chance_status);
		param.add(id);
	    dao.exculeByParams(hql, param);
	}

	@Override
	public void delete_prize_chance(int id) throws Exception {
		// TODO Auto-generated method stub
		String hql="update Prize_chance set status=0 where id=?";
		List<Object> param=new ArrayList<Object>();
		param.add(id);
	    dao.exculeByParams(hql, param);
	}

	@Override
	public PageBean search_prize_chance(Map<String, Object> map, int pageSize, int page) throws Exception {
		// TODO Auto-generated method stub
		StringBuffer hql=new StringBuffer("from Prize_chance as p where 1=1");
		List<Object> param = new ArrayList<Object>();
		if(map.get("name")!=null && map.get("name")!=""){
			hql.append(" and p.user.name like ?");
			param.add("%"+map.get("name")+"%");
		}
		if(map.get("chance_status")!=null && map.get("chance_status")!="")
		{
			hql.append(" and p.chance_status=?");
			param.add(Integer.parseInt((String)map.get("chance_status")));
		}
		int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int currentPage = PageBean.countCurrentPage(page);
		List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);

		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;
	}

	@Override
	public Prize_chance get_prize_chance(int id) throws Exception {
		// TODO Auto-generated method stub
		return (Prize_chance)dao.load(Prize_chance.class, id);
	}

	@Override
	public void update_chance_status(int id) throws Exception {
		// TODO Auto-generated method stub
		String hql="update Prize_chance set chance_status=1 where id=?";
		List<Object> param=new ArrayList<Object>();
		param.add(id);
	    dao.exculeByParams(hql, param);
	}

}
